<%@page import="org.springframework.http.HttpRequest"%>
<%@page import="org.votes.bean.Details"%>
<%@page import="org.votes.bean.Manager"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <%
    	Manager manager = (Manager) request.getAttribute("manager");
    	List<Details> list = (List<Details>) request.getAttribute("list");
     %>
     
    <title>My JSP 'result.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<script type="text/javascript" src="<%=basePath %>/jquery/jquery-1.4.4.min.js"></script>
	<script type="text/javascript">
	<!--
		function resultHiden(){
			$("#result").hide();//hide()函数,实现隐藏
			$("#add").show();//显示
		}
		function addHiden(){
			$("#add").hide();//hide()函数,实现隐藏
			$("#result").show();//显示
		}
		
		/* function generator() {
			window.location.href='${pageContext.request.contextPath }/doCreate.do';
			alert('生成成功');
		} */
		$(document).ready(function() {
			$("#generator").click(function(){
			$.post("${pageContext.request.contextPath }/doCreate.do",
				
				function (data){
					 if(data == "OK") {
					 	alert('生成成功！');
					 	addHiden();
					 }
				}); 
            }); 
		});
		
		function findVotes() {
			window.location.href='${pageContext.request.contextPath }/details/lookVotes.do';
		}
		
		$(document).ready(function() {
			$("#addDetail").click(function(){
				var title = $.trim($("#title").val());
				var answer1 = $.trim($("#answer1").val());
				var answer2 = $.trim($("#answer2").val());
				var answer3 = $.trim($("#answer3").val());
				var answer4 = $.trim($("#answer4").val());
				if(title.length == 0) {
					alert('请填写标题');
					return false;
				}
			$.post("${pageContext.request.contextPath }/details/addDetails.do",
				{	title : title,
					answer1 : answer1,
					answer2 : answer2,
					answer3 : answer3,
					answer4 : answer4,
				},
				function (data){
					 if(data == "OK") {
					 	alert('添加成功！');
					 	addHiden();
					 }else {
					 	alert('添加失败！');
					 	$("#title").val("");
					 	$("#answer1").val("");
					 	$("#answer2").val("");
					 	$("#answer3").val("");
					 	$("#answer4").val("");
					 }
				}); 
            }); 
		}); 
	//-->
	</script>
  </head>
  
  <body onload="addHiden()">
  		<div id="result">
			<table border="1" align="center">
				<tr>
					<td>标题</td>
					<td>答案1</td>
					<td>答案2</td>
					<td>答案3</td>
					<td>答案4</td>
				<tr>
				<c:forEach items="${list }" var="detail">
				<tr>
					<td>${detail.title }</td>
					<td>${detail.answer1 }</td>
					<td>${detail.answer2 }</td>
					<td>${detail.answer3 }</td>
					<td>${detail.answer4 }</td>
				<tr>
				</c:forEach>
				<tr>
					<td colspan="5" align="center">
						<input type="button" onclick="resultHiden()" value="添加题目" >
						&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
						<!-- onclick="generator()" -->
						<input type="button" onclick="generator()" id="generator" value="生成静态文件" >
						&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
						<input type="button" onclick="findVotes()" value="查看投票结果" >
					</td>
				</tr>
			</table>
		</div>
		
		<div id="add">
			<table border="1" align="center">
				<tr>
					<td>标题:</td>
					<td>
						<input type="text" id="title">
					</td>
				<tr>
				<tr>
					<td>答案1:</td>
					<td>
						<input type="text" id="answer1">
					</td>
				<tr>
				<tr>
					<td>答案2:</td>
					<td>
						<input type="text" id="answer2">
					</td>
				<tr>
				<tr>
					<td>答案3:</td>
					<td>
						<input type="text" id="answer3">
					</td>
				<tr>
				<tr>
					<td>答案4:</td>
					<td>
						<input type="text" id="answer4">
					</td>
				<tr>
				<tr>
					<td colspan="2" align="center">
						<input type="button" value="提交" id="addDetail">
					</td>
				<tr>
			</table>
		</div>
  </body>
</html>
